<template>
  <!-- 选择元素 -->
  <el-dialog :title="selectInfo.title" :close-on-click-modal="false" v-model="selectInfo.open"
             width="1000px" append-to-body>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px" label-position="right" label-suffix=":">
      <el-row>
        <el-col :span="9">
          <el-form-item label="元素名称" prop="elementNameLike">
            <el-input v-model="queryParams.elementNameLike" placeholder="请输入元素名称" clearable
                      @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="元素编码" prop="elementCodeLike">
            <el-input v-model="queryParams.elementCodeLike" placeholder="请输入元素编码" clearable
                      @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <search-button @click="handleQuery" :perm="['rule:config:element:search']" />
            <reset-button @click="resetQuery" :perm="['rule:config:element:search']" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="parentElementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="元素名称" prop="elementName" :show-overflow-tooltip="true" min-width="180" />
      <el-table-column label="元素编码" prop="elementCode" :show-overflow-tooltip="true" min-width="310" />
      <el-table-column label="映射类型" prop="mappingType" align="center" min-width="80">
        <template v-slot="scope">{{ selectDictLabel(mappingTypeOptions, scope.row.mappingType) }}</template>
      </el-table-column>
      <el-table-column label="是否有效" prop="isEnable" align="center" min-width="80">
        <template v-slot="scope">{{ selectDictLabel(isEnableOptions, scope.row.isEnable) }}</template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page="queryParams.pageNum" :limit="queryParams.pageSize"
                @pagination="getParentElementList"
                style="display: block"  />
    <dialog-footer class="dialog-footer" @ok="submitForm" @cancel="cancel"  />

  </el-dialog>
</template>

<script>
import {debounceWrapper} from "@/utils/zmids.ts";
import {insert, insertBatch, updateById} from "@/platform/uni/api/uni/element/index.ts";
import {elementListByParentCategoryCode} from "@/platform/uni/api/uni/element/index.ts";

export default {
  name: "selectComponent",
  props: ['selectForm', 'mappingTypeOptions', 'isEnableOptions'],
  data() {
    return {
      loading: false,
      parentElementList: [],
      insertParam:{
        ids:'',
        cateId:'',
      },
      // 总条数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 元素编码
        elementCodeLike: '',
        // 元素名称
        elementNameLike: '',
      },
      selectInfo: {},
    }
  },
  mounted() {
    this.selectInfo = this.selectForm
    console.log('selectInfo',this.selectInfo)
  },
  methods: {
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.elementId);
    },
    // 提交按钮
    submitForm: debounceWrapper(function () {
      //批量插入
      //将ids用逗号拼接
      this.insertParam.ids = this.ids.join(',');
      this.insertParam.cateId = this.selectInfo.cateId;
      insertBatch(this.insertParam).then(response => {
        this.selectInfo.open = false;
        this.$message.success("选择成功");
        //刷新页面 todo
        this.$emit('getList');
      });

    }),
    // 取消按钮
    cancel() {
      this.selectInfo.open = false;
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getParentElementList();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //获取父分类绑定的元素
    getParentElementList() {
      this.loading = true;
      this.queryParams.parentCategoryCode = this.selectInfo.parentCategoryCode;
      this.queryParams.cateId = this.selectInfo.cateId;
      elementListByParentCategoryCode(this.queryParams).then(
        response => {
          // debugger
          if (response.data) {
            this.parentElementList = response.data.list;
            this.total = response.data.total;
          }
          this.loading = false;
        }
      );
    },
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  display: block;
  padding: 90px 0 0 0;
  text-align: right;
}
</style>
